Ext.define('DragonApp.view.ban.Controller', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.ban',

    config: {
        isBan: undefined
    },
    //查询
    onSubmitClick: function(button, e, options) {

        var me = this;

        var form = this.getView().getForm();
        if (!form.isValid()) return;

        var formValue = form.getValues();

        var grid = Ext.ComponentQuery.query('bangrid')[0];
        var store = grid.getStore();

        //数据仓库根据查询附加参数加载
        store.load({
            params: formValue,
            scope: this,
            callback: function(records, operation, success) {
                if (success == false) {
                    store.removeAll();
                }
            }
        });
    },
    //回车响应
    onTextFieldSpecialKey: function(field, e, options) {
        if (e.getKey() === e.ENTER) {
            if (this.getView().getForm().isValid()) {
                this.onSubmitClick();
            }
        }
    },
    //==================
    //封号及禁言操作请求
    //==================
    onRequest: function(button, e, options) {

        var grid = this.getView().up('grid');
        var store = grid.getStore();

        var records = grid.getSelectionModel().getSelection();

        if(0 == records.length){
            DragonApp.util.Msg.showToast('提示：请先选择封禁对象');
            return;
        }

        //批量获取ID
        var arr = [];

        records.forEach(function(item) {
            arr.push(item.id);
        });

        var param = {};
        param.id = JSON.stringify(arr);

        var url, title;

        this.setIsBan(DragonApp.util.Regular.BAN.test(button.itemId));

        switch (button.itemId) {

            case 'banAccount':
                url = DragonApp.ux.config.Config.getUrl('/index/Ban/banAccount/');
                title = '账号封禁';
                break;

            case 'banComment':
                url = DragonApp.ux.config.Config.getUrl('/index/Ban/banComment/');
                title = '角色禁言';
                break;

            case 'unbanAccount':
                url = DragonApp.ux.config.Config.getUrl('/index/Ban/unbanAccount/');
                title = '账号解封';
                break;

            case 'unbanComment':
                url = DragonApp.ux.config.Config.getUrl('/index/Ban/unbanComment/');
                title = '角色禁言解除';
                break;
        }

        this.batPrompt(url, param, records, store, title);
    },
    //=========
    //操作提示
    //=========
    batPrompt: function(url, param, records, store, title) {

        var me = this;

        if (false == this.getIsBan()) {
            Ext.Msg.show({
                title: '提示',
                msg: '确认' + title + '?',
                buttons: Ext.Msg.YESNO,
                icon: Ext.Msg.QUESTION,
                fn: function(buttonId) {
                    if (buttonId == 'yes') {
                        me.jsonpBatRequest(url, param, records, store);
                    }
                }
            });
        } else if (true == this.getIsBan()) {
            var arr = [];

            records.forEach(function(item) {
                arr.push(item.id);
            });

            var view = me.getView().up('grid');

            me.dialog = view.add({
                xtype: 'ban_window',
                height: 250,
                width: 380,
                title: title
            });

            var idfield = Ext.ComponentQuery.query('hiddenfield', me.dialog)[0];
            idfield.setValue(JSON.stringify(arr));

            //弹窗附加
            me.dialog.setUrl(url);
            me.dialog.setRecords(records);
            me.dialog.show();
        }
    },
    //========
    //封禁操作
    //========
    onSave: function(button, e, options) {

        //封禁用户仓库
        var grid = Ext.ComponentQuery.query('bangrid')[0];
        var store = grid.getStore();

        //修改角色禁言状态
        var form = button.up('window').down('form').getForm();
        var param = form.getValues();

        var url = button.up('window').getUrl();
        var records = button.up('window').getRecords();

        if (form.isValid()) {
            this.jsonpBatRequest(url, param, records, store);
        }
    },
    //========
    //取消封禁
    //========
    onCancel: function(button, e, options) {

        button.up('window').destroy();
    },
    //===================
    //JsonP[批量操作]请求
    //===================
    jsonpBatRequest: function(url, param, records, store) {

        //封禁窗体
        var win = Ext.ComponentQuery.query('ban_window')[0];
        var grid = Ext.ComponentQuery.query('bangrid')[0];

        maskComponent = win ? win : grid;
        maskComponent.mask('请稍候...');

        Ext.data.JsonP.request({
            url: url,
            async: false,
            params: param,
            callbackKey: "callback",
            success: function(result) {

                maskComponent.unmask(); //完成等待提示
                switch (result.success) {
                    case true:
                        if (win) {
                            win.destroy();
                        }
                        DragonApp.util.Msg.showToast(result.msg);
                        var rec = [];
                        for (var i = 0; i < records.length; i++) {
                            rec.push(records[i].get('id'));
                        }
                        store.load({
                            params: { id: rec.toString() }
                        });
                        break;

                    case false:
                        DragonApp.util.Msg.showToast(result.msg);
                        break;

                    default:
                        DragonApp.util.Msg.showToast(result.msg);
                        break;
                }
            },
            failure: function(result) {
                DragonApp.util.Msg.showToast(result);
            }
        });
    },
    //============
    //内容提示渲染
    //============
    attrRender: function(value, metadata, record, rowIndex, columnIndex, store) {

        if (typeof(value) == "undefined") {
            value = '';
            metadata.tdAttr = 'data-qtip="' + value + '" ' + 'data-qalign="bl-br"' + 'data-qwidth="auto"';
            return value;
        } else {
            metadata.tdAttr = 'data-qtip="' + value + '" ' + 'data-qalign="bl-br"' + 'data-qwidth="auto"';
            return value;
        }
    },
    //================
    //账号封禁状态渲染
    //================
    accountRender: function(value, metadata, record, rowIndex, columnIndex, store) {

        if (0 == value) {
            return "已封号";
        } else if (1 == value) {
            return "正常";
        }
    },
    //================
    //账号禁言状态渲染
    //================
    commentRender: function(value, metadata, record, rowIndex, columnIndex, store) {

        if (1 == value) {
            return "已禁言";
        } else if (0 == value) {
            return "正常";
        }
    }
});